<template>
  <div class="filters">
    <el-row type="flex" class="filters-top" justify="space-between" align="middle">
      <el-col :span="8">
        单程：
        {{$route.query.departCity}} - {{$route.query.destCity}}
        /
        {{$route.query.departDate}}
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="airport" placeholder="起飞机场" @change="runFilterList">
          <el-option
            v-for="(airport,index) in options.airport"
            :key="index"
            :label="airport"
            :value="airport"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="flightTimes" placeholder="起飞时间" @change="runFilterList">
          <el-option
            v-for="(time,index) in options.flightTimes"
            :key="index"
            :label="`${time.from}:00-${time.to}:00`"
            :value="`${time.from},${time.to}`"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="company" placeholder="航空公司" @change="runFilterList">
          <el-option
            v-for="(company,index) in options.company"
            :key="index"
            :label="company"
            :value="company"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="airSize" placeholder="机型" @change="runFilterList">
          <el-option
            v-for="(size,index) in planeSize"
            :key="index"
            :label="size.label"
            :value="size.value"
          ></el-option>
        </el-select>
      </el-col>
    </el-row>
    <div class="filter-cancel">
      筛选：
      <el-button type="primary" round plain size="mini" @click="handleFiltersCancel">撤销</el-button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: Object,
    origin: Array
  },
  data() {
    return {
      airport: "", // 机场
      flightTimes: "", // 出发时间
      company: "", // 航空公司
      airSize: "", // 机型大小
      planeSize: [
        {
          label: "大",
          value: "L"
        },
        {
          label: "中",
          value: "M"
        },
        {
          label: "小",
          value: "S"
        }
      ]
    };
  },
  mounted() {
    console.log(this.$route.query);
    console.log(this.origin);
  },
  methods: {
    runFilterList() {
      let newList = [...this.origin];
      // 选择机场时候
      if (this.airport) {
        newList = newList.filter(flight => {
          return flight.org_airport_name == this.airport;
        });
      }
      // 选择出发时间
      if (this.flightTimes) {
        newList = newList.filter(flight => {
          const fromDepHour = Number(flight.dep_time.split(":")[0]);
          const fromHour = Number(this.flightTimes.split(",")[0]);
          const toHour = Number(this.flightTimes.split(",")[1]);
          return fromDepHour >= fromHour && fromDepHour < toHour;
        });
      }
      // 选择航空公司
      if (this.company) {
        newList = newList.filter(flight => {
          return flight.airline_name == this.company;
        });
      }
      // 选择机型大小
      if (this.airSize) {
        newList = newList.filter(flight => {
          return flight.plane_size == this.airSize;
        });
      }
      this.$emit("setFlightsList", newList);
    },

    // 撤销条件时候触发
    handleFiltersCancel() {
      this.airport = "";
      this.flightTimes = "";
      this.company = "";
      this.airSize = "";
      this.runFilterList();
    }
  }
};
</script>

<style scoped lang="less">
.filters {
  margin-bottom: 20px;
}

.filters-top {
  > div {
    /deep/ .el-select {
      margin-left: 10px;
    }
  }
}

.filter-cancel {
  margin-top: 10px;
}
</style>